package com.reebake.ideal.document.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.document.entity.DocumentTemplate;
import com.reebake.ideal.document.service.DocumentTemplatePlusService;
import com.reebake.ideal.document.service.DocumentTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DocumentTemplatePlusServiceImpl implements DocumentTemplatePlusService {
    @Autowired
    private DocumentTemplateService documentTemplateService;

    @Override
    @Transactional
    public void saveOrUpdate(DocumentTemplate documentTemplate, String content) {
        String bizType = documentTemplate.getBizType();
        if(StrUtil.isNotBlank(bizType) && StrUtil.isBlank(documentTemplate.getDocumentTemplateId())){
            DocumentTemplate temp = queryByBizType(bizType);
            if(ObjectUtil.isNotNull(temp)){
                documentTemplate.setDocumentTemplateId(temp.getDocumentTemplateId());
            }
        }
        documentTemplateService.saveOrUpdate(documentTemplate);
    }

    @Override
    public DocumentTemplate queryByBizType(String bizType) {
        LambdaQueryWrapper<DocumentTemplate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DocumentTemplate::getBizType, bizType);
        return documentTemplateService.getOne(queryWrapper);
    }

}
